잠시만 기다려 주세요

     '시장이나 돌면서 가짜 민생 외치는 이재명을 규탄한다. 싸우지 않을 거면 내려와라... 야당대표는 당신의 방탄이나 하는 자리가 아니다.'
전체검색 :  
이번주 로또 및 연금번호 발생!!   |  HOME   |  여기는?   |  바다물때표   |  알림 (16)  |  여러가지 팁 (1054)  |  추천 및 재미 (150)  |  자료실 (22)  |  
시사, 이슈, 칼럼, 평론, 비평 (582)  |  끄적거림 (127)  |  문예 창작 (702)  |  바람 따라 (69)  |  시나리오 (760)  |  드라마 대본 (248)  |  
살인!


    golang

golang - golang 파일 다운로드 예제... Force download file example
이 름 : 바다아이   |   조회수 : 8873         짧은 주소 : https://www.bada-ie.com/su/?411591780315
// 아래는 파일을 읽어서 뿌리는 방식.... 
// 다른 방법으로는 url 로 불러서 뿌리는 방식도 있습니다. url 로 불러 뿌리는 게 좀 더 많이 사용될 겁니다.

package main

 import (
         "bytes"
         "fmt"
         "io/ioutil"
         "net/http"
         "strconv"
         "time"
 )

 // change to your target filename
 // that you want to force download to your visitor browser
 var file = "img.pdf"

 func Home(w http.ResponseWriter, r *http.Request) {

         // We will force download after 2 seconds. If you want to increase the delay
         // simply change 2 to whatever number you wanted.

         html := "<html><meta http-equiv='refresh' content='2; url=http://localhost:8080/download' />"
         html = html + "Downloading file now. If the download does not happen automagically. Please "
         html = html + "<a href=" + file + ">click here</a></html>"
         w.Write([]byte(html))
 }

 func ForceDownload(w http.ResponseWriter, r *http.Request) {

         downloadBytes, err := ioutil.ReadFile(file)

         if err != nil {
                 fmt.Println(err)
         }

         // set the default MIME type to send
         mime := http.DetectContentType(downloadBytes)

         fileSize := len(string(downloadBytes))

         // Generate the server headers
         w.Header().Set("Content-Type", mime)
         w.Header().Set("Content-Disposition", "attachment; filename="+file+"")
         w.Header().Set("Expires", "0")
         w.Header().Set("Content-Transfer-Encoding", "binary")
         w.Header().Set("Content-Length", strconv.Itoa(fileSize))
         w.Header().Set("Content-Control", "private, no-transform, no-store, must-revalidate")

         //b := bytes.NewBuffer(downloadBytes)
         //if _, err := b.WriteTo(w); err != nil {
         //              fmt.Fprintf(w, "%s", err)
         //      }

         // force it down the client's.....
         http.ServeContent(w, r, file, time.Now(), bytes.NewReader(downloadBytes))

 }

 func main() {
         http.HandleFunc("/", Home)
         http.HandleFunc("/download", ForceDownload)

         // SECURITY : Only expose the file permitted for download.
         http.Handle("/"+file, http.FileServer(http.Dir("./")))

         http.ListenAndServe(":8080", nil)
 }


출처 : https://www.socketloop.com/tutorials/golang-force-download-file-example
| |





      1 page / 6 page
번 호 카테고리 제 목 이름 조회수
179 golang golang , ... 바다아이 1307
178 golang golang , map . 바다아이 1042
177 golang Golang (, , data ) , ... 바다아이 1046
176 golang golang sort ... 바다아이 1215
175 golang golang html.EscapeString html.UnescapeString input value ... 바다아이 1385
174 golang golang go.mod go.sum . GOPATH SRC not module, 1.16 . 바다아이 4527
173 golang go 1.16 ... is not in GOROOT.. GOPATH .... . 바다아이 5364
172 golang , String Formatting 바다아이 6940
171 golang rand.Intn , random, , . 바다아이 6525
170 golang golang ... 바다아이 9277
169 golang golang gopath, goroot .. golang 바다아이 7111
현재글 golang golang ... Force download file example 바다아이 8874
167 golang golang , , cpu, memory, disk 바다아이 10160
166 golang golang , ... GOOS, GOARCH 바다아이 8046
165 golang golang checkbox ... 바다아이 7757
164 golang golang , , http .... 바다아이 7576
163 golang golang nil , nil , nil ... 바다아이 7843
162 golang 2 golang, go , .... golang .... 바다아이 10636
161 golang golang postgresql, mysql, mariadb ... ` Grave () .. .. 바다아이 8098
160 golang golang postgresql mysql, mariadb scan , null .. 바다아이 8198
159 golang golang , iconv 바다아이 10992
158 golang golang quote escape, unquote 바다아이 8365
157 golang golang , http errorLog , , ... 바다아이 8482
156 golang golang interface , 바다아이 7987
155 golang golang struct .... 바다아이 8657
154 golang golang map map , 바다아이 8223
153 golang golang map .... .... 바다아이 7743
152 golang golang slice copy 바다아이 7834
151 golang golang goto 바다아이 8671
150 golang golang slice sort , int, string, float64 바다아이 8102
| |









Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천  실행시간 : 0.05641
to webmaster... gogo sea. gogo sea.